<?php
/*
 * @Author: Terry
 * @Date: 2024-01-31 13:16:44
 * @LastEditors: Terry
 * @LastEditTime: 2024-11-12 20:50:54
 * @Description: file content
 * @FilePath: \auxface_api\app\certificate\controller\admin\v1\Form.php
 */

declare (strict_types = 1);

namespace app\certificate\controller\admin\v1;

use think\facade\Db;
use think\Request;

class Form{
   /**
    * @Description: 
    * @Author: Terry
    * @Date: 2024-01-31 16:41:47
    * @param {Request} $request
    */    
    public function getData(Request $request){
        $data = $request->data;
        $data = DB::table('certificate_formTemplate')->where('uuid',$data['uuid'])->value('fields');
        $returnData = [
            'formData'=>[
                'fields'=>json_decode(html_entity_decode($data),true) ?? [],
            ],
        ];
        return result(SUCCESS,$returnData);
    }
    /**
     * @Description: 
     * @Author: Terry
     * @Date: 2024-05-18 22:54:37
     * @param {Request} $request
     */    
    public function element(Request $request){
        $element  = DB::table('certificate_formElement')->where('isDelete',0)->field(['value','type'])->select()->toArray();
        foreach ($element as $key => $value) {
            $data[$value['type']][] =json_decode($value['value'],true);
        }
        return result(SUCCESS,$data);
    }

    /**
     * @Description:添加 
     * @Author: Terry
     * @Date: 2024-05-21 18:05:33
     * @param {Request} $request
     */
    public function create(Request $request){
        $data = $request->data;
        $field = 'formUuid';
        $tables =[
            'machine'=>'certificate_machineDigitizationTemplate',
            'risk'=>'certificate_riskType',
            'checklistDay'=>'certificate_heavyMachineCheckType',
            'checklistweek'=>'certificate_heavyMachineCheckType',
            'checklistAudit'=>'certificate_heavyMachineCheckType',
        ];
        if(!isset($tables[$data['type']]))     return result(FAIL,'添加失败，请刷新重试'); 
        if($data['type'] == 'checklistDay') $field = 'dayFormUuid';
        if($data['type'] == 'checklistweek') $field = 'weekFormUuid';
        if($data['type'] == 'checklistAudit') $field = 'auditFormUuid';
        $tableName = $tables[$data['type']];
        $tableData = DB::table($tableName)->where(['uuid'=>$data['uuid'],'isDelete'=>0])->field(['projectCode','corpCode'])->find();
        if(!$tableData) return result(FAIL,'添加失败，请刷新重试'); 
        $tableUuid = $data['uuid'];
        $data = [
            'fields'=> $data['fields'],
            'uuid'=>createUuid(),
            'type'=>$data['type'],
            'projectCode'=>$tableData['projectCode'],
            'corpCode'=>$tableData['corpCode'],
        ];

        DB::startTrans();
        try {
            $res = DB::table('certificate_formTemplate')->insert($data);
            if($res) $updateRes = DB::table($tableName)->where(['uuid'=>$tableUuid])->update([$field=>$data['uuid']]);
            if($res && $updateRes){
                DB::commit();
                return result(SUCCESS,[$field=>$data['uuid']],'添加成功');
            }
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
        }
        return result(FAIL,'添加失败，请刷新重试'); 
    }
    
    /**
     * @Description: 修改
     * @Author: Terry
     * @Date: 2024-01-31 21:02:26
     */
    public function update(Request $request){
        $data = $request->data;
        DB::table('certificate_formTemplate')->where('uuid',$data['formUuid'])->update([
            'fields'=> $data['fields'],
        ]);
        return result(SUCCESS,'','更改成功');
    }
    public function createElement(){
        // $data = [
        //     'label'=>'添加关联数据',
        //     'tag'=>'el-button',
        //     'tagIcon'=>'',
        //     'defaultValue'=>'',
        //     'readonly'=>true,
        //     'style'=>[],
        //     'optionType'=>'default',
        //     'border'=>false,
        //     'size'=>'medium',
        //     'disabled'=>false,
        //     'required'=>false,
        //     'type'=>[
        //         [
        //             'label'=>'工人',
        //             'value'=>'worker'
        //         ], 
        //         [
        //             'label'=>'班组',
        //             'value'=>'team'
        //         ],
        //         [
        //             'label'=>'参建单位',
        //             'value'=>'corp'
        //         ]
        //     ],
        //     'changeTag'=>true,
        // ];
        // $insert = [
        //     'zhCnName'=>'添加关联数据',
        //     'value'=>json_encode($data),
        //     'type'=>'common',
        //     'uuid'=>createUuid(),
        // ];
        // DB::table('certificate_formElement')->insert($insert);
    }
}
